<template>
  <!--优秀案例征集新增  -->
  <div>
    <div>
      <FormPanel ref="formPanel" :list="initForm" :cols="2" :formType="type" :labelWidth="130" :isSubmitBtn="false" @formChange="formChangeHandle" />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">关 闭</el-button>
      <el-button :loading="zfLoading" v-if="type == 'edit'" @click="zfDialog" type="primary">作 废</el-button>
      <el-button :loading="saveLoading" v-if="type == 'add' || type == 'edit'" @click="saveDialog" type="primary">保 存</el-button>
      <el-button :loading="publishLoading" v-if="type == 'add' || type == 'edit'" @click="publishDialog" type="primary">发 布</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { formatdateByDay } from '@/utils/index';
import { addCaseCollect, updateCaseCollect, selectListByBill } from '@/api/cms-api/projectOperating/case/oemCollect/index';
export default {
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      required: true
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      initForm: this.initFormFun(),
      caseUrlList: [],
      formValue: {},
      saveLoading: false, //保存按钮Loading
      publishLoading: false, //发布按钮Loading
      fileRes: {},
      zfLoading: false //作废按钮Loading
    };
  },
  mounted() {
    // console.log(this.value); //获取到行id 、类型（新增、详情）
    //请求接口进行赋值，自己写吧
  },
  mounted() {
    this.init();
  },
  methods: {
    async file() {
      this.fileRes = await selectListByBill({ caseLevyId: this.rowData.caseLevyId, billType: 25681001 });
      if (this.fileRes.data.length > 0) {
        let caseUrlList = [];
        for (let j = 0; j < this.fileRes.data.length; j++) {
          let o1 = { name: this.fileRes.data[j].fileName };
          let o2 = { url: this.fileRes.data[j].fileId };
          let a = Object.assign(o1, o2);
          caseUrlList.push(a);
        }
        this.initForm[7].initialValue = caseUrlList;
      }
    },
    init() {
      if (this.type === 'edit') {
        // 更新接口需要的记录 ID
        this.caseLevyId = this.rowData.caseLevyId;
        //查询附件
        this.file();
        // console.log(this.rowData);
        this.initForm.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
      if (this.type === 'show') {
        //查询附件
        this.file();
        this.initForm.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
    },
    //搜索数据
    initFormFun() {
      return [
        {
          type: 'INPUT',
          label: '优秀案例征集编号',
          fieldName: 'caseLevyCode',
          initialValue: '系统自动生成',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '优秀案例征集名称',
          fieldName: 'caseLevyName',
          initialValue: '',
          rules: [
            { required: true, message: '请输入征集名称', trigger: 'blur' },
            { max: 30, message: '征集名称长度不能超过30', trigger: 'blur' }
          ]
        },
        {
          type: 'DATE',
          label: '征集开始日期',
          fieldName: 'levyStartDate',
          initialValue: '',
          valueFormat: 'yyyy-MM-dd',
          rules: [{ required: true, message: '请输入征集开始日期', trigger: 'blur' }]
        },
        {
          type: 'DATE',
          label: '征集结束日期',
          fieldName: 'levyEndDate',
          initialValue: '',
          valueFormat: 'yyyy-MM-dd',
          rules: [{ required: true, message: '请输入征集结束日期', trigger: 'blur' }]
        },
        {
          type: 'DATE',
          label: '项目执行开始日期',
          fieldName: 'projectExecuteStartDate',
          initialValue: '',
          valueFormat: 'yyyy-MM-dd',
          rules: [{ required: true, message: '请输入项目执行开始日期', trigger: 'blur' }]
        },
        {
          type: 'DATE',
          label: '项目执行结束日期',
          fieldName: 'projectExecuteEndDate',
          initialValue: '',
          valueFormat: 'yyyy-MM-dd',
          rules: [{ required: true, message: '请输入项目执行结束日期', trigger: 'blur' }]
        },
        {
          type: 'TEXT_AREA',
          label: '方案征集要求及说明',
          fieldName: 'caseLevyClaim',
          initialValue: '',
          rows: 4,
          selfCols: 2,
          rules: [{ required: true, message: '请输入方案征集要求及说明', trigger: 'blur' }]
        },
        {
          type: 'UPLOAD_FILE',
          label: '附件',
          disabled: this.type === 'show' ? true : false,
          fieldName: 'caseUrl',
          initialValue: '',
          upload: {
            actionUrl: '/api/file/oss/upload',
            fileTypes: ['pdf', 'jpg', 'png', 'rar'],
            limit: 10,
            max: 1024 * 100
          }
        }
      ];
    },
    formChangeHandle(val) {
      this.formValue = val;
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (null == this.$refs.formPanel.SUBMIT_FORM()) {
        return;
      }
      if (this.formValue != null) {
        if (formatdateByDay(this.formValue.levyStartDate) > formatdateByDay(this.formValue.levyEndDate)) {
          this.$notify.warning({ title: '提示信息', message: '征集开始时间不能大于征集结束时间!' });
          return false;
        }
        if (formatdateByDay(this.formValue.projectExecuteStartDate) > formatdateByDay(this.formValue.projectExecuteEndDate)) {
          this.$notify.warning({ title: '提示信息', message: '活动执行开始时间不能大于活动执行结束时间!' });
          return false;
        }
        this.saveLoading = true;
        //追加隐藏状态字段
        this.formValue = Object.assign({}, { caseLevyStatus: 25331001 }, this.formValue);
        let res = {};
        //新增保存
        if (this.type == 'add') {
          if (this.formValue.caseUrl != null) {
            for (let i = 0; i < this.formValue.caseUrl.length; i++) {
              this.caseUrlList.push(Object.assign({}, { name: this.formValue.caseUrl[i].name }, { url: this.formValue.caseUrl[i].url }));
            }
            this.formValue = Object.assign({}, { fileUrl: this.caseUrlList }, this.formValue);
          }
          res = await addCaseCollect(this.formValue);
          if (res.resultCode === 200) {
            this.saveLoading = false;
            this.$notify.success({ title: '提示信息', message: '新增成功!' });
            this.closeDialog('refresh');
          }
        }
        //编辑保存
        if (this.type == 'edit') {
          if (this.formValue.caseUrl != null) {
            for (let i = 0; i < this.formValue.caseUrl.length; i++) {
              this.caseUrlList.push(Object.assign({}, { name: this.formValue.caseUrl[i].name }, { url: this.formValue.caseUrl[i].url }));
            }
            this.formValue = Object.assign({}, { fileUrl: this.caseUrlList }, this.formValue);
          }
          res = await updateCaseCollect(this.formValue, this.caseLevyId);
          if (res.resultCode === 200) {
            this.saveLoading = false;
            this.$notify.success({ title: '提示信息', message: '编辑成功!' });
            this.closeDialog('refresh');
          }
        }
      }
    },
    async publishDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (null == this.$refs.formPanel.SUBMIT_FORM()) {
        return;
      }
      if (this.formValue != null) {
        if (formatdateByDay(this.formValue.levyStartDate) > formatdateByDay(this.formValue.levyEndDate)) {
          this.$notify.warning({ title: '提示信息', message: '征集开始时间不能大于征集结束时间!' });
          return false;
        }
        if (formatdateByDay(this.formValue.projectExecuteStartDate) > formatdateByDay(this.formValue.projectExecuteEndDate)) {
          this.$notify.warning({ title: '提示信息', message: '活动执行开始时间不能大于活动执行结束时间!' });
          return false;
        }
        this.publishLoading = true;
        //追加隐藏状态字段
        this.formValue = Object.assign({}, { caseLevyStatus: 25331002 }, this.formValue);
        let res = {};
        //新增发布
        if (this.type == 'add') {
          if (this.formValue.caseUrl != null) {
            for (let i = 0; i < this.formValue.caseUrl.length; i++) {
              this.caseUrlList.push(Object.assign({}, { name: this.formValue.caseUrl[i].name }, { url: this.formValue.caseUrl[i].url }));
            }
            this.formValue = Object.assign({}, { fileUrl: this.caseUrlList }, this.formValue);
          }
          res = await addCaseCollect(this.formValue);
          if (res.resultCode === 200) {
            this.publishLoading = false;
            this.$notify.success({ title: '提示信息', message: '发布成功!' });
            this.closeDialog('refresh');
          }
        }
        //编辑发布
        if (this.type == 'edit') {
          if (this.formValue.caseUrl != null) {
            for (let i = 0; i < this.formValue.caseUrl.length; i++) {
              this.caseUrlList.push(Object.assign({}, { name: this.formValue.caseUrl[i].name }, { url: this.formValue.caseUrl[i].url }));
            }
          }
          this.formValue = Object.assign({}, { fileUrl: this.caseUrlList }, this.formValue);
          res = await updateCaseCollect(this.formValue, this.caseLevyId);
          if (res.resultCode === 200) {
            this.publishLoading = false;
            this.$notify.success({ title: '提示信息', message: '发布成功!' });
            this.closeDialog('refresh');
          }
        }
      }
    },
    //作废
    async zfDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (null == this.$refs.formPanel.SUBMIT_FORM()) {
        return;
      }
      if (this.formValue !== null) {
        this.zfLoading = true;
        //追加隐藏状态字段
        this.formValue = Object.assign({}, { caseLevyStatus: 25331003 }, this.formValue);
        let res = {};
        if (this.type == 'edit') {
          if (this.formValue.caseUrl != null) {
            for (let i = 0; i < this.formValue.caseUrl.length; i++) {
              this.caseUrlList.push(Object.assign({}, { name: this.formValue.caseUrl[i].name }, { url: this.formValue.caseUrl[i].url }));
            }
            this.formValue = Object.assign({}, { fileUrl: this.caseUrlList }, this.formValue);
          }
          res = await updateCaseCollect(this.formValue, this.caseLevyId);
          if (res.resultCode === 200) {
            this.zfLoading = false;
            this.$notify.success({ title: '提示信息', message: '已作废!' });
            this.closeDialog('refresh');
          }
        }
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    }
  }
};
</script>
